package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.LinkBean;
import com.project.service.ILinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lyl
 * description:联系人模块控制器
 */
@Controller
@RequestMapping("link")
public class LinkController {
    @Autowired
    private ILinkService iLinkService;

    @RequestMapping("findAllByPage")
    public String finAllByPage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, HttpServletRequest request) {
        PageInfo<LinkBean> pageInfo = iLinkService.findAllByPage(pageNo);
        request.setAttribute("pageInfo", pageInfo);
        //请求转发至此link包下的index.html网页
        return "/html/link/index.html";
    }

    @RequestMapping("add")
    public String add(LinkBean linkBean) {
        iLinkService.add(linkBean);
        //使用重定向避免请求转发造成的不断重复
        return "redirect:/link/findAllByPage";
    }

    @RequestMapping("del")
    public String del(int id) {
        iLinkService.del(id);
        return "redirect:/link/findAllByPage";
    }

    @RequestMapping("findById")
    public String findById(int id, Model model) {
        LinkBean linkBean = iLinkService.findById(id);
        model.addAttribute("linkObj", linkBean);
        return "/html/link/update.html";
    }

    @RequestMapping("update")
    public String update(int id, String tel) {
        iLinkService.updateTel(id, tel);
        return "redirect:/link/findAllByPage";
    }

}
